using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace FBPChaos
{
    public class Monster
    {
        int attack;
        int lawChaos;
        int manoeuvre;
        bool flying;
        int flyingMovement;
        string monsterDescription;
        string monsterName;
        bool mount;
        int movement;
        Point position;
        bool ranged;
        int rangedAttack;
        int range;
        bool undead;
        bool alive;
        bool illusion;
        int defense;
        int magicResistance;
        bool selectable;

        int zIndex; //int representing top monster on stack, other monsters will be dead

        public Monster(int a, int d, int mr, int l, int man, bool isflying, int flym, string desc, string name, bool ismount, int m, bool isranged, int ra, int ran, bool isundead)
        {
            attack = a;
            lawChaos = l;
            manoeuvre = man;
            flying = isflying;
            flyingMovement = flym;
            monsterDescription = desc;
            monsterName = name;
            mount = ismount;
            movement = m;
            ranged = isranged;
            rangedAttack = ra;
            range = ran;
            undead = isundead;
            illusion = false;
            defense = d;
            magicResistance = mr;
            alive = false;
            zIndex = 0;
            selectable = false;
        }

        public Monster(string line)
        {
            //find semi colon
            //take string between semi colon and first character or semi colon and previous semi colon and assign to value; 
            int first = 0;
            int last;
            int numberOfAttributes = 15;
            string[] attribute;
            attribute = new string[numberOfAttributes];
            try
            {
                last = line.IndexOf(";");

                for (int counter = 0; counter < numberOfAttributes; counter++)
                {
                    attribute[counter] = line.Substring(first, last - first);
                    first = last + 1;
                    last = line.IndexOf(";", first);
                }
                monsterName = attribute[0];
                movement = int.Parse(attribute[1]);
                defense = int.Parse(attribute[2]); ;
                attack = int.Parse(attribute[3]);
                magicResistance = int.Parse(attribute[4]);
                lawChaos = int.Parse(attribute[5]);
                manoeuvre = int.Parse(attribute[6]);
                flying = bool.Parse(attribute[7]);
                flyingMovement = int.Parse(attribute[8]); ;
                mount = bool.Parse(attribute[9]);
                ranged = bool.Parse(attribute[10]);
                rangedAttack = int.Parse(attribute[11]);
                range = int.Parse(attribute[12]);
                undead = bool.Parse(attribute[13]);
                monsterDescription = attribute[14];
                illusion = false;
                alive = false;
                zIndex = 0;
                selectable = false;
            }
            catch (Exception e)
            {

            }

        }

        public Monster(Monster monsterToCopy)
        {
            attack = monsterToCopy.attack;
            lawChaos = monsterToCopy.lawChaos;
            manoeuvre = monsterToCopy.manoeuvre;
            flying = monsterToCopy.flying;
            flyingMovement = monsterToCopy.flyingMovement;
            monsterDescription = monsterToCopy.monsterDescription;
            monsterName = monsterToCopy.monsterName;
            mount = monsterToCopy.mount;
            movement = monsterToCopy.movement;
            position = monsterToCopy.position;
            ranged = monsterToCopy.ranged;
            rangedAttack = monsterToCopy.rangedAttack;
            undead = monsterToCopy.undead;
            alive = monsterToCopy.alive;
            illusion = monsterToCopy.illusion;
            defense = monsterToCopy.defense;
            magicResistance = monsterToCopy.magicResistance;
            selectable = monsterToCopy.selectable;
        }

        /* public void createMonster()
         {
             throw new NotImplementedException();
         }*/

        /// <summary>
        /// Kills the monster
        /// </summary>
        /// <returns>Returns true if monster destroyed (i.e. undead), otherwise returns false</returns>
        public bool killMonster()
        {
            alive = false;
            if (undead)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Deals with the specifics of combat and returns the result
        /// </summary>
        /// <param name="attackedMonster">Reference to monster being attacked</param>
        /// <returns>Returns true if successful, false if not</returns>
        public bool attackCreature(Monster attackedMonster)
        {
            return combat(attack, attackedMonster.Defense);
        }

        /// <summary>
        /// Deals with the specifics of combat and returns the result
        /// </summary>
        /// <param name="attackedWizard">Reference to wizard being attacked</param>
        /// <returns>Returns true if successful, false if not</returns>
        public bool attackCreature(Wizard attackedWizard)
        {
            return combat(attack, attackedWizard.Defense);
        }

        private bool combat(int attackRating, int defenseRating)
        {
            Random randomNumber = new Random();
            if (attackRating + randomNumber.Next(1, 6) > defenseRating + randomNumber.Next(1, 6))
                return true;
            else
                return false;
        }

        public void moveMonster(Point point, int newZIndex)
        {
            position = point;
            zIndex = newZIndex;
        }

        public void resurrectMonster()
        {
            alive = true;
            undead = true;
        }

        public string MonsterName
        {
            get { return monsterName; }
            set { monsterName = value; }
        }

        public Point Position
        {
            get { return position; }
        }

        public int ZIndex
        {
            get { return zIndex; }
            set { zIndex = value; }
        }

        public int Defense
        {
            get { return defense; }
        }
    }
}
